home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / PERMUTE.TST / ANLZPMTN.C next >
C/C++ Source or Header  |  1996-02-23  |  3KB  |  116 lines

  1. /* ============ */
  2. /* anlzpmtn.c    */
  3. /* ============ */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pmtndefs.h>
  7.  
  8. /* ------------------- */
  9. /* FUNCTION PROTOTYPES */
  10. /* ------------------- */
  11. # undef F
  12. # if defined(__STDC__) || defined(__PROTO__)
  13. #    define  F( P )  P
  14. # else
  15. #    define  F( P )  ()
  16. # endif
  17.  
  18. /* INDENT OFF */
  19. static    int    AlreadyHaveValue F((int *, int, int));
  20. extern    int    AnalyzeNextPermutation F((PRMUT_DATA_STRU *));
  21. static    int    MaxOf F((int *, int));
  22.  
  23. # undef F
  24. /* INDENT ON */
  25.  
  26. #define    SWAP_INT(T, a, b)    \
  27.      {                \
  28.     int    x = T[a];    \
  29.     T[a] = T[b];        \
  30.     T[b] = x;        \
  31.      }
  32. #define    PRINT_TABLE(T, n)                    \
  33.     {                                \
  34.     int    k;                              \
  35.     for (k = 1; k <= n; ++k)                \
  36.     {                                       \
  37.         P(printf("\t%5d%10d\n", k, T[k-1]));    \
  38.     }                               \
  39.     }
  40. /* ==================================================================== */
  41. /* AnalyzeNextPermutation - Converts t random numbers to unique number    */
  42. /* ==================================================================== */
  43. int
  44. AnalyzeNextPermutation(PRMUT_DATA_STRU *PermuteData)
  45. {
  46.     int     k;
  47.     int     NextNum, UniqNum = 0;
  48.     int     UniqTbl[MAX_ELEMS];
  49.  
  50.     for (k = 0; k < PermuteData->NumElements; ++k)
  51.     {
  52.     do
  53.     {
  54.         NextNum = PermuteData->RandFun();
  55.         ++PermuteData->NumVariates;
  56.     }
  57.     while (AlreadyHaveValue(UniqTbl, k, NextNum));
  58.  
  59.     P(printf("NextNum = %5d\n", NextNum));
  60.     UniqTbl[k] = NextNum;
  61.     }
  62.  
  63.     P(printf("Before Transformation: Table =\n"));
  64.     PRINT_TABLE(UniqTbl, PermuteData->NumElements);
  65.     for (k = PermuteData->NumElements; k > 1; --k)
  66.     {
  67.     int    s = MaxOf(UniqTbl, k);
  68.     UniqNum = k * UniqNum + s;
  69.     SWAP_INT(UniqTbl, k-1, s);
  70.     }
  71.     P(printf("After  Transformation: Table =\n"));
  72.     PRINT_TABLE(UniqTbl, PermuteData->NumElements);
  73.  
  74.     return (UniqNum);
  75. }
  76. /* ==================================================================== */
  77. /* AlreadyHaveValue - Returns true if number at Value is in Table    */
  78. /* ==================================================================== */
  79. static
  80. int    AlreadyHaveValue(int *Table, int NumElems, int Value)
  81. {
  82.     int     k;
  83.  
  84.     for (k = NumElems; k >= 1; --k)
  85.     {
  86.     if (Table[k-1] == Value)
  87.     {
  88.         break;
  89.     }
  90.     }
  91.     P(printf("AlreadyHaveValue Returning %s\n",
  92.     (k > 0) ? "TRUE" : "FALSE"));
  93.  
  94.     return (k);
  95. }
  96. /* ==================================================================== */
  97. /* MaxOf - Returns Index of Maximum Value in Table of NumElems Elements    */
  98. /* ==================================================================== */
  99. static
  100. int    MaxOf(int *Table, int NumElems)
  101. {
  102.     int  j, k, Max;
  103.     j = 0;
  104.     Max = *Table;
  105.     for (k = 1; k < NumElems; ++k)
  106.     {
  107.     if (Max < Table[k])
  108.     {
  109.         j = k;
  110.         Max = Table[k];
  111.     }
  112.     }
  113.     P(printf("MaxOf Returning %5d, Max = %5d\n", j, Table[j]));
  114.     return j;
  115. }
  116.